import axios from 'axios'
// import { Message } from 'element-ui'
// import store from '@/store'
// import { getToken } from '@/utils/auth'

// create an axios instance
const service = axios.create({
  baseURL: 'http://127.0.0.1:9999', // api 的 base_url
  timeout: 5000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    // Do something before request is sent
    if (localStorage.getItem("token")) {
      // 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改
      config.headers['Authorization'] = localStorage.getItem("token")
    }
    return config
  },
  error => {
    // // Do something with request error
    // console.log(error) // for debug
    Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  res => {
    if(res.data.message == '身份认证失败！') {
      window.location.href = "/";
    }
    return res
    // if (res.request.responseType === 'arraybuffer' && res.headers['content-disposition']) {
    //   //当存在content-disposition这个字段时，return出来的数据
    //   const temp = {}
    //   temp.data = res.data
    //   temp.filename = res.headers['content-disposition']
    //   return temp
    // }
  },
  error => {

    // Message({
    //   message: error.message,
    //   type: 'error',
    //   duration: 5 * 1000
    // })
    return Promise.reject(error)
  }
)

export default service
